package com.hb.trade.server.expert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.expert.dto.ExpertDTO;
import com.hb.core.pojo.expert.dto.ExpertGroupMembersDTO;
import com.hb.core.pojo.expert.entity.ExpertGroup;
import com.hb.core.pojo.expert.entity.ExpertGroupMembers;
import com.hb.core.pojo.expert.mapper.ExpertBaseMapper;
import com.hb.core.pojo.expert.mapper.ExpertGroupMapper;
import com.hb.core.pojo.expert.mapper.ExpertGroupMembersMapper;
import com.hb.trade.server.expert.service.ExpertGroupMembersService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.IdUtil;
import org.fzzn.core.util.MsgUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ljs
 * @date 2022-2-10 14:13:46
 **/
@Service
public class ExpertGroupMembersServiceImpl extends ServiceImpl<ExpertGroupMembersMapper, ExpertGroupMembers> implements ExpertGroupMembersService {

    @Resource
    private ExpertBaseMapper expertBaseMapper;
    @Resource
    private ExpertGroupMapper expertGroupMapper;

    /**
     * 查询抽取专家的记录
     *
     * @param expertGroupMembersDTO
     * @return org.fzzn.core.model.msg.Message<java.util.List < com.hb.core.pojo.expert.dto.ExpertGroupMembersDTO>>
     * @author tian
     * @date 2022-4-14 16:00
     */
    @Override
    public Message<List<ExpertGroupMembersDTO>> selectExpertGroupMembersByGroupId(ExpertGroupMembersDTO expertGroupMembersDTO) {
        ExpertGroup expertGroup = new ExpertGroup();
        expertGroup.setId(expertGroupMembersDTO.getGroupId());
        List<ExpertGroupMembersDTO> expertGroupMembersDTOList = expertGroupMapper.listExpertGroupMembers(expertGroup);
        return MsgUtil.success(expertGroupMembersDTOList);
    }


    /**
     * 补充抽取专家
     *
     * @param expertGroupMembersDTO
     * @return org.fzzn.core.model.msg.Message<java.lang.ExpertDTO>
     * @author tian
     * @date 2022-4-14 16:08
     */
    @Override
    @Transactional
    public Message<String> supplementExtract(ExpertGroupMembersDTO expertGroupMembersDTO) {
        // 待补抽添加的的专家
        List<ExpertGroupMembersDTO> supplementExtractList = expertGroupMembersDTO.getSupplementExtractList();
        // 待删除的的专家
        List<Long> deleteExpertGroupMembersIdList = expertGroupMembersDTO.getDeleteExpertGroupMembersIdList();
        for (Long id : deleteExpertGroupMembersIdList) {
            baseMapper.deleteById(id);
        }
        // 添加待补抽添加的的专家
        for (ExpertGroupMembersDTO groupMembersDTO : supplementExtractList) {
            ExpertGroupMembers expertGroupMembers = new ExpertGroupMembers();
            groupMembersDTO.to(expertGroupMembers);
            expertGroupMembers.setId(IdUtil.getId());
            expertGroupMembers.setGroupId(expertGroupMembersDTO.getGroupId());
            expertGroupMembers.setExpertMajor(groupMembersDTO.getMajorCode());
            expertGroupMembers.setPhoneNum(groupMembersDTO.getPhoneNumber());
            expertGroupMembers.setCreateTime(LocalDateTime.now());
            expertGroupMembers.setCreateId(expertGroupMembersDTO.getCreateId());
            expertGroupMembers.setCreateName(expertGroupMembersDTO.getCreateName());
            expertGroupMembers.setDeleteFlag(0);
            baseMapper.insert(expertGroupMembers);
        }

        // 标记最一次的抽取时间
        ExpertGroup expertGroup = new ExpertGroup();
        expertGroup.setId(expertGroupMembersDTO.getGroupId());
        expertGroup.setCreateTime(LocalDateTime.now());
        expertGroup.setUpdateTime(LocalDateTime.now());
        expertGroup.setUpdateId(expertGroupMembersDTO.getCreateId());
        expertGroup.setUpdateName(expertGroupMembersDTO.getCreateName());
        expertGroupMapper.updateById(expertGroup);

        return MsgUtil.success();
    }
}
